var util = require('../../utils/utils.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab:0,
    product:[],
    times:[],
    hours: '0'+0,   // 时
    minute: '0' + 0,   // 分
    second: '0' + 0  ,  // 秒
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.showTime();
     this.switchTime();
    this.setInterval();
    //获取手机高度
    this.setData({
      navH: app.globalData.navHeight,
      partnerUserId: options.partnerUserId
    })
 },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
        } else {
          wx.navigateTo({
            url: '/pages/zcuslist/index?partnerUserId=' + that.data.partnerUserId + '&fan=1'
          })
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 页面分享
   */
  onShareAppMessage: function () {
    var that = this;
    return {
      title: that.data.title,
      desc: '分享页面的内容',
      imageUrl: "http://file.jwsxmall.com/shareImg.jpg",
      path: '/pages/seckill/seckill?partnerUserId=' + wx.getStorageSync("userId") + '&type=2', // 路径，传递参数到指定页面
    }
  },
  switchTime:function(e){
    var that = this;
    var time = 0;
    if (typeof (e) != "undefined"){
        time = this.data.times[e.target.dataset.current];
       if (this.data.currentTab === e.target.dataset.current) {
        return false;
      } else {
        that.setData({
          currentTab: e.target.dataset.current,//获取data-current的值
        })
      }
      }else{
        time = this.data.times[0];
      that.setData({
        currentTab:0,//获取data-current的值
      })
      }
    var timeNext = time+1-(new Date()).getHours();
    var min = 59-(new Date()).getMinutes();
    var sceond = 60 - (new Date()).getSeconds();
    if (timeNext < 10){
      timeNext = '0'+timeNext;
    }
    if (min < 10) {
      min = '0' + min;
    }
    that.setData({
      hours: timeNext,
      minute: min,
      second: sceond,
    })
    wx.request({
      url: app.globalData.url + 'getSkillProduct', //
      data: {
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          product:res.data.data,
        })
        console.log(res.data);
      },
      fail: function (err) {
        
      },
    })
  },
  /**
   * 时间动态
   */
  showTime: function () {
    var that = this;
    //获取当前时间
    var time = util.formatTime(new Date())
    var num = parseInt(time);
    //为页面中time赋值
    var arr = [];
    for (var i = 0; i < 5 ; i++) {
      if ((num + 2) > 24) {
        arr[i] = '00';
        continue;
      }
      if (num % 2 == 0 && i == 0) {
        arr[i] = num;
        num = num + 2;
      } else if (num % 2 == 0) {
        num = num + 2
        arr[i] = num;
      } else {
        num = num - 1;
        arr[i] = num; 
      }
    }
    that.setData({
      times:arr
    })
  },
  goToProduct:function(e){
      wx.navigateTo({
        url: '../productDetail/productDetail?productid=' + e.currentTarget.dataset.id+"&type=1",
      })
  },
  // 计时器
  setInterval: function () {
    const that = this
    var second = that.data.second
    var minute = that.data.minute
    var hours = that.data.hours
    let timer = setInterval(function () {  // 设置定时器
      second--
      if (second <= 0) {
        second = 60  //  大于等于60秒归零
        minute--
        if (minute <= 0) {
          minute = 59  //  大于等于60分归零
          hours--
          that.setData({
            hours: '0' + hours
          })
          if(hours < 0){
            clearInterval(timer);
            that.onLoad();
          }
        }
        if (minute < 10) {
          // 少于10补零
          that.setData({
            minute: '0' + minute
          })
        } else {
          that.setData({
            minute: minute
          })
        }
      
      }
      
      if (second < 10) {
        // 少于10补零
        that.setData({
          second: '0' + second
        })
      } else {
        that.setData({
          second: second
        })
      }
    }, 1000)
  },
  navBack: function () {
    wx.switchTab({
      url: '/pages/activityPage/activityPage',
    })
  }

})